let Home = {
    baseUrl: 'http://81.71.65.4:3003',
    /**
     * 轮播图渲染模块
     */
    renderSw() {
        let html = ''
        axios.get(this.baseUrl + '/product/getBanners').then((res) => {
            // console.log(res);
            let data = res.data.banners
            data.forEach(function(v, i) {
                // console.log(v);
                html += `<img src="${v}">`
            })
            document.querySelector('.lb').innerHTML = html + `<img src="${data[0]}">`
        })
    },
    /**
     * 导航栏渲染模块
     */
    renderNav() {
        let html = ''
        axios.get(this.baseUrl + '/category/all').then((res) => {
            // console.log(res);
            let data = res.data.list
            data.forEach((v, i) => {
                    // console.log(v);
                    html += `<a href="./fenlei.html?sid=${v.categoryId}" sid=${v.categoryId}>
                <div class="box">
                    <div><img src="${v.iconUrl}"></div>
                    <p>${v.name}</p>
                </div>
            </a>`
                })
                // console.log(html);
            document.querySelector('#index .nav').innerHTML = html
            this.focusClick()
        }).catch(() => {
            console.log('请求错误');
        })
    },

    /**
     * 商品渲染 模块
     */
    renderShopp() {
        let sid = localStorage.getItem('sid')
        let html = ''
        axios.post(this.baseUrl + '/product/list', { categoryId: sid, currPage: 0 }).then((res) => {
            let data = res.data.list
            data.forEach((v, i) => {
                // console.log(v);
                html += `<a href="./xiangqing.html?sid=${v.productId}">
                    <li>
                        <img src="${v.imgUrl}">
                        <p class="s1">${v.masterName}</p>
                        <p class="s2">${v.categoryName}</p>
                        <div class="num">
                            <p class="s3">
                                <span class="n1">￥</span>
                                <span class="num1">${v.minPrice/100}</span>
                            </p>
                            <p class="s4">
                                <span class="n2">销量：</span>
                                <span class="num2">9998</span>
                            </p>
                        </div>
                    </li>
                </a>`
            })
            document.querySelector('#shop ul').innerHTML = html
        }).catch(() => {
            console.log('请求错误');
        })
    },
    focusClick() {
        let as = document.querySelectorAll('.nav a')
        for (let i = 0; i < as.length; i++) {
            as[i].addEventListener('focus', () => {
                let sid = as[i].getAttribute('sid')
                localStorage.setItem('sid', sid)
            })
        }
    }
}

onload = function() {
    Home.renderSw()

    Home.renderNav()

    Home.renderShopp()
}